package com.example.multithread.copyonwrite;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.CopyOnWriteArrayList;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

/**
 * @ClassName CopyOnWriteDemo
 * @Description TODO
 * @Author leejiliang
 * @DateTime 2020/5/14 10:41 AM
 * @Version 1.0
 */
public class CopyOnWriteDemo {

	private static Logger logger = LoggerFactory.getLogger(CopyOnWriteDemo.class);
	public static void main(String[] args) throws InterruptedException {
		CopyOnWriteArrayList<Integer> copyOnWriteArrayList = new CopyOnWriteArrayList();
		CopyOnWriteArrayList copyOnWriteArrayList1 = new CopyOnWriteArrayList();
		IntStream.rangeClosed(1, 100).forEach(copyOnWriteArrayList::add);
		Thread thread1 = new Thread(() -> copyOnWriteArrayList.forEach(i -> logger.info(i.toString())));
		Thread thread2 = new Thread(() -> copyOnWriteArrayList.add(8888888));
		thread1.start();
		thread2.start();
		thread1.join();
		thread2.join();
	}
}
